package com.dtdream.shop.service;

import com.dtdream.pojo.TbSeller;
import com.dtdream.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuxj on 2018/3/20.
 * 自定义认证类
 */
public class UserDetailServiceImpl implements UserDetailsService {

    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("进入了UserDetailsServiceImpl");
        //创建列表角色
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
        //获取商家对象
        TbSeller seller = sellerService.findOne(username);
        if(seller != null && seller.getStatus().equals("1")){
            return new User(username,seller.getPassword(),grantedAuths);
        }else {
            return null;
        }
    }
}
